import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { UserRole, UserStatus, type Notification, type UserInfo } from '@/types/user'
import type { App, AppCategory } from '@/types/app'

// 模拟用户数据
const mockUserInfo: UserInfo = {
  id: 1,
  username: 'demo',
  nickname: '测试用户',
  email: 'demo@example.com',
  avatar: '/avatars/default.png',
  role: UserRole.USER,
  status: UserStatus.ENABLED,
  createdAt: '2023-01-01T00:00:00Z',
  updatedAt: '2023-01-01T00:00:00Z'
}

// 模拟通知
const mockNotifications: Notification[] = [
  {
    id: 1,
    title: '欢迎使用 Simple Tools Pro',
    content: '感谢您使用我们的应用，如有问题请随时联系我们。',
    time: '刚刚',
    read: false
  },
  {
    id: 2,
    title: '新应用可用',
    content: '有3个新应用可供安装，快去应用市场看看吧！',
    time: '1小时前',
    read: false
  },
  {
    id: 3,
    title: '系统更新',
    content: '系统已更新到最新版本，享受更流畅的体验。',
    time: '昨天',
    read: true
  }
]

// 模拟应用数据
const mockApps: App[] = [
  {
    id: 1,
    name: '记事本',
    version: '1.0.0',
    description: '简单的文本编辑工具',
    icon: '/icons/notepad.png',
    categoryId: 1,
    developerId: 1,
    developerName: 'System',
    status: 1,
    isFeatured: true,
    config: {
      window: {
        width: 800,
        height: 600,
        resizable: true,
        maximizable: true,
        minimizable: true
      },
      permissions: []
    },
    storagePath: '/apps/notepad',
    views: 0,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 2,
    name: '计算器',
    version: '1.0.0',
    description: '数学计算工具',
    icon: '/icons/calculator.png',
    categoryId: 4,
    developerId: 1,
    developerName: 'System',
    status: 1,
    isFeatured: true,
    config: {
      window: {
        width: 320,
        height: 480,
        resizable: false,
        maximizable: false,
        minimizable: true
      },
      permissions: []
    },
    storagePath: '/apps/calculator',
    views: 0,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 3,
    name: '待办事项',
    version: '1.0.0',
    description: '任务管理工具',
    icon: '/icons/todo.png',
    categoryId: 1,
    developerId: 1,
    developerName: 'System',
    status: 1,
    isFeatured: true,
    config: {
      window: {
        width: 600,
        height: 500,
        resizable: true,
        maximizable: true,
        minimizable: true
      },
      permissions: []
    },
    storagePath: '/apps/todo',
    views: 0,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 4,
    name: '图片查看器',
    version: '1.0.0',
    description: '查看和编辑图片',
    icon: '/icons/image-viewer.png',
    categoryId: 3,
    developerId: 1,
    developerName: 'System',
    status: 1,
    isFeatured: true,
    config: {
      window: {
        width: 900,
        height: 600,
        resizable: true,
        maximizable: true,
        minimizable: true
      },
      permissions: []
    },
    storagePath: '/apps/image-viewer',
    views: 0,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  }
]

// 模拟应用分类
const mockCategories: AppCategory[] = [
  { id: 1, name: '办公工具', code: 'office', description: '提高办公效率的工具', sort: 1, status: 1 },
  { id: 2, name: '开发工具', code: 'development', description: '软件开发相关工具', sort: 2, status: 1 },
  { id: 3, name: '设计工具', code: 'design', description: '设计创作相关工具', sort: 3, status: 1 },
  { id: 4, name: '实用工具', code: 'utility', description: '日常使用的实用工具', sort: 4, status: 1 }
]

export const useMockStore = defineStore('mock', () => {
  // 通知列表
  const notifications = ref<Notification[]>([...mockNotifications])
  
  // 获取模拟应用列表
  const getApps = (): App[] => {
    return mockApps
  }

  // 获取模拟分类
  const getCategories = (): AppCategory[] => {
    return mockCategories
  }

  // 获取模拟通知
  const getNotifications = () => {
    return notifications.value
  }

  // 未读通知数量
  const unreadNotificationCount = computed(() => {
    return notifications.value.filter(notification => !notification.read).length
  })

  // 获取用户信息
  const getUserInfo = () => {
    return mockUserInfo
  }

  // 将通知标记为已读
  const markNotificationAsRead = (id: number) => {
    const notification = notifications.value.find(n => n.id === id)
    if (notification) {
      notification.read = true
    }
  }

  // 将所有通知标记为已读
  const markAllNotificationsAsRead = () => {
    notifications.value.forEach(notification => {
      notification.read = true
    })
  }

  return {
    getApps,
    getCategories,
    getNotifications,
    unreadNotificationCount,
    getUserInfo,
    markNotificationAsRead,
    markAllNotificationsAsRead
  }
}) 